package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.demo.entity.po.Post;
import com.example.demo.entity.po.User;
import com.example.demo.entity.po.StatsOverview;
import com.example.demo.mapper.PostMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.StatsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;

@Service
public class StatsServiceImpl implements StatsService {

    @Autowired
    private UserMapper userMapper;
    
    @Autowired
    private PostMapper postMapper;

    @Override
    public StatsOverview getOverview() {
        StatsOverview overview = new StatsOverview();
        
        // 获取总用户数
        LambdaQueryWrapper<User> userWrapper = new LambdaQueryWrapper<>();
        overview.setTotalUsers(Integer.valueOf(Math.toIntExact(userMapper.selectCount(userWrapper))));
        
        // 获取总内容数
        LambdaQueryWrapper<Post> postWrapper = new LambdaQueryWrapper<>();
        overview.setTotalContent(Math.toIntExact(postMapper.selectCount(postWrapper)));
        
        // 获取活跃用户数（30天内有发帖的用户）
        Date thirtyDaysAgo = Date.from(LocalDateTime.now().minusDays(30)
            .atZone(ZoneId.systemDefault()).toInstant());
        overview.setActiveUsers(postMapper.countActiveUsers(thirtyDaysAgo));
        
        // 获取今日发帖数
        Date today = Date.from(LocalDate.now().atStartOfDay()
            .atZone(ZoneId.systemDefault()).toInstant());
        LambdaQueryWrapper<Post> todayWrapper = new LambdaQueryWrapper<>();
        overview.setTodayPosts(Math.toIntExact(postMapper.selectCount(todayWrapper)));
        
        return overview;
    }

    @Override
    public List<Map<String, Object>> getActiveUserTrend() {
        // 获取最近7天的数据
        List<Map<String, Object>> trend = new ArrayList<>();
        String[] days = {"周一", "周二", "周三", "周四", "周五", "周六", "周日"};
        
        // 获取每天的发帖用户数
        List<Map<String, Object>> dailyStats = postMapper.getDailyActiveUsers(7);
        
        // 找出最大值用于计算百分比
        int maxValue = dailyStats.stream()
            .mapToInt(map -> ((Number)map.get("value")).intValue())
            .max()
            .orElse(1);
            
        for (int i = 0; i < dailyStats.size(); i++) {
            Map<String, Object> item = dailyStats.get(i);
            item.put("day", days[i]);
            int value = ((Number)item.get("value")).intValue();
            item.put("percentage", (int)(value * 100.0 / maxValue));
        }
        
        return dailyStats;
    }

    @Override
    public List<Map<String, Object>> getContentTypes() {
        // 从数据库获取内容分类统计
        return postMapper.getContentTypeStats();
    }

    @Override
    public List<Map<String, Object>> getHotTopics() {
        // 获取最近一周的热门话题
        Date weekAgo = Date.from(LocalDateTime.now().minusDays(7)
            .atZone(ZoneId.systemDefault()).toInstant());
        return postMapper.getHotTopics(weekAgo);
    }

    @Override
    public List<Map<String, Object>> getAgeDistribution() {
        // 获取用户年龄分布
        return userMapper.getAgeDistribution();
    }
}